package com.sinodata.bsm.cicp.finder.tuxedo;

import java.io.BufferedReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.finder.Finder;
import com.sinodata.bsm.cicp.util.SnmpConnect;

public class TuxedoDomainFinder extends Finder {
    private static Logger logger = Logger.getLogger(TuxedoDomainFinder.class);

    //    public List find(ResValue resValue, ResAttributeValValue[] attrVals, ResAttributeValue[] attributes) {
    //        List retList = new ArrayList();
    //        String result = null;
    //        BufferedReader reader = null;
    //        String ip = host;
    //        String TableOIDS = ".1.3.6.1.4.1.140.300.3.1.0";
    //        String community = "public";
    //        String regex = "\\s*(.*)#";
    //        Pattern pattern = Pattern.compile(regex);
    //        try {
    //            result = SnmpConnect.getValueBySnmpTable(ip, TableOIDS, community);
    //            StringReader strReader = new StringReader(result);
    //            reader = new BufferedReader(strReader);
    //            String readLine = null;
    //            while ((readLine = reader.readLine()) != null) {
    //                Matcher matcher = pattern.matcher(readLine);
    //                if (matcher.find()) {
    //                    retList.add(creatRes(matcher, attributes));
    //                }
    //            }
    //            logger.info("tuxedo queue find successed");
    //        } catch (Exception e) {
    //            logger.error("failed to find tuxedo queue", e);
    //        }
    //        System.out.println(retList.size());
    //        return retList;
    //    }
    //
    //    private List creatRes(Matcher matcher, ResAttributeValue[] attributes) {
    //        List aRes = new ArrayList();
    //        ResAttributeValValue valValue = com.union.pub.util.CollectorUtil.getResAttrValValue(attributes[0].getId(), matcher.group(1));
    //        ResValue resValue = new ResValue();
    //        resValue.setIp(host);
    //        resValue.setName(valValue.getAttrValue());
    //        resValue.setInternalId(matcher.group(1));
    //        aRes.add(resValue);
    //        aRes.add(valValue);
    //        return aRes;
    //    }

    @SuppressWarnings("rawtypes")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> resList = new ArrayList<String[]>();
        String result = null;
        BufferedReader reader = null;
        String TableOIDS = ".1.3.6.1.4.1.140.300.3.1.0";
        String community = "public";
        String regex = "\\s*(.*)#";
        try {
            result = SnmpConnect.getValueBySnmpTable(ip, TableOIDS, community);
            StringReader strReader = new StringReader(result);
            reader = new BufferedReader(strReader);
            String strLine = null;
            Pattern pattern = Pattern.compile(regex);
            while ((strLine = reader.readLine()) != null) {
                Matcher matcher = pattern.matcher(strLine);
                if (matcher.find()) {
                    String domainName = matcher.group(1);
                    String[] ss = new String[4];
                    resList.add(ss);
                    ss[0] = "tuxedo_domain_" + domainName;
                    ss[1] = "tuxedo_domain_" + domainName;
                    ss[2] = "tuxedo_domain_" + domainName;
                    ss[3] = matcher.group(1);
                }
            }
            return resList;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("fail to find tuxedo domain", e);
            return null;
        }
    }

}
